package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.PropertySetService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.PropertySet;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PropertySetServiceImpl implements PropertySetService {

    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;




    /**
     * 获取资产列表
     *
     *
     * */
    @Override
    public ZhongkaiResult getProList(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PropertySet.class);
        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);
        return ZhongkaiResult.ok(pageBean);
    }


    /**
     * 删除资产
     *
     * @param id 资产id
     */
    @Override
    public ZhongkaiResult deletePro(String id) {
        if (StringUtils.isNullOrEmpty(id)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        //PropertySet
        PropertySet propertySet = (PropertySet) iBaseDao.get("from PropertySet ps where ps.id=" + id + "");

        if (propertySet == null){

            return ZhongkaiResult.build(400, "资产已被删除");
        }


        iBaseDao.delete(propertySet);


        return ZhongkaiResult.ok();
    }

    @Override
    public ZhongkaiResult updataPro() {
        return null;
    }

    /**
     * 新增资产
     *
     * @param property_name 资产名称
     * @param unit_price    单价
     * @param unit_count    数量
     * @param new_time      购置时间
     * @param pro_remark    资产备注信息
     */

    @Override
    public ZhongkaiResult addPro(String property_name,
                                 String unit_price,
                                 Integer unit_count,
                                 Long new_time,
                                 String pro_remark) {
        if (StringUtils.isNullOrEmpty(property_name) || StringUtils.isNullOrEmpty(unit_price)

                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        PropertySet propertySet = (PropertySet) iBaseDao.get("from PropertySet ps where ps.propertyName='" + property_name + "'");

        if (propertySet != null) {
            return ZhongkaiResult.build(400, "资产名称重复 请重命名");
        }

        // long millis = new DateTime().getMillis(); //获取当前系统时间戳
        propertySet = new PropertySet();


        propertySet.setPropertyName(property_name);
        propertySet.setUnitPrice(unit_price);

        propertySet.setUnitCount(unit_count);

        propertySet.setNewTime(new_time);
        propertySet.setProRemark(pro_remark);

        iBaseDao.save(propertySet);


        return ZhongkaiResult.ok();
    }
}
